<template>
  <div class="login-container">
    <div class="login-box">
      <h1 class="login-title">用户登录</h1>
      <el-form :model="form" label-width="100px" :rules="rules" ref="form" style="margin-top: 50px">
        <el-form-item label="用户名" prop="name">
          <el-input type="text" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password" show-password></el-input>
        </el-form-item>
        <el-form-item style="margin-top: 20px">

          <el-button type="primary" class="login-button" @click="Login" style=" width: 300px">登 录
          </el-button>
          <el-button type="success" class="register-button" @click="Register" style="width: 300px">注 册
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>


import RegisterForm from "@/components/Register.vue";

export default {
  name: 'LoginComponent',
  computed: {
    RegisterForm() {
      return RegisterForm
    }
  },
  data() {
    return {
      form: {
        name: "",
        password: "",
      },
      rules: {
        name: [{
          required: true, message: "请输入账号", trigger: "blur"
        }],
        password: [{
          required: true, message: "请输入密码", trigger: "blur"
        }],
      }
    };
  },
  methods: {

    Login() {

      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$axios.post(this.$httpUrl + "/user/login/",this.form)
            .then((res) => res.data)
            .then((res) => {
              console.log(res);
              if (res.code === 200) {
                this.$store.commit("setUser", JSON.stringify(res.data.user))
                this.$store.commit("setMenu", JSON.stringify(res.data.menu))
                this.$message({
                  message: '登录成功',
                  type: 'success',
                  onClose: () => {
                    this.$router.push("/");
                  },
                });
              } else {
                this.$message({
                  message: res.msg,
                  type: 'error'
                });
              }
            });
        } else {
          this.$message({
            message: "校验失败",
            type: 'error'
          });
        }
      })
    },
    Register() {
      this.$router.push("/Register");

    }
  },
};
</script>

<style>
.login-container {
  align-items: center;
  justify-content: center;
  height: 100vh;
  display: flex;
  background-image: url(../assets/1.webp);
  background-size: 100% 100%;
}

.login-box {
  width: 400px;
  border-radius: 6px;
  padding: 30px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: relative;
  backdrop-filter: blur(10px);
  text-align: center;
}


.login-title {
  margin: 20px 0;
  text-align: center;
}


.login-button {
  width: 80%;
  margin: 0 auto;
  display: block;
}
</style>
